<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="VirtualBox， Vagrant安装VirtualBox, 安装vagrant 安装Box你可以把它想成是一个箱子，里面装了一些东西。在用 Vagrant 创建虚拟机的时候，需要用到 Box ，它里面会包装操作系统的镜像，不同的 Box 带的操作系统可能是不一样的，比如 CentOS，Ubuntu 等等，你可以基于它们去创建自己版本的 Box，比如在虚拟机上安装一些软件，然后把它重新打包成">
<meta property="og:type" content="article">
<meta property="og:title" content="vagrant usage">
<meta property="og:url" content="http://changyuan.github.io/2016/11/10/vagrant/index.html">
<meta property="og:site_name" content="Chang Crazy&#39;s Blog">
<meta property="og:description" content="VirtualBox， Vagrant安装VirtualBox, 安装vagrant 安装Box你可以把它想成是一个箱子，里面装了一些东西。在用 Vagrant 创建虚拟机的时候，需要用到 Box ，它里面会包装操作系统的镜像，不同的 Box 带的操作系统可能是不一样的，比如 CentOS，Ubuntu 等等，你可以基于它们去创建自己版本的 Box，比如在虚拟机上安装一些软件，然后把它重新打包成">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-11-10T06:46:53.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vagrant usage">
<meta name="twitter:description" content="VirtualBox， Vagrant安装VirtualBox, 安装vagrant 安装Box你可以把它想成是一个箱子，里面装了一些东西。在用 Vagrant 创建虚拟机的时候，需要用到 Box ，它里面会包装操作系统的镜像，不同的 Box 带的操作系统可能是不一样的，比如 CentOS，Ubuntu 等等，你可以基于它们去创建自己版本的 Box，比如在虚拟机上安装一些软件，然后把它重新打包成">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://changyuan.github.io/2016/11/10/vagrant/">





  <title>vagrant usage | Chang Crazy's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chang Crazy's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">升级自己的操作系统</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://changyuan.github.io/2016/11/10/vagrant/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="changyuan">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chang Crazy's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">vagrant usage</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-11-10T14:46:53+08:00">
                2016-11-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="VirtualBox，-Vagrant"><a href="#VirtualBox，-Vagrant" class="headerlink" title="VirtualBox， Vagrant"></a>VirtualBox， Vagrant</h3><p>安装<a href="https://www.virtualbox.org/wiki/Downloads" target="_blank" rel="noopener">VirtualBox</a>, 安装<a href="https://www.vagrantup.com/downloads.html" target="_blank" rel="noopener">vagrant</a></p>
<h3 id="安装Box"><a href="#安装Box" class="headerlink" title="安装Box"></a>安装Box</h3><p>你可以把它想成是一个箱子，里面装了一些东西。在用 Vagrant 创建虚拟机的时候，需要用到 Box ，它里面会包装操作系统的镜像，不同的 Box 带的操作系统可能是不一样的，比如 CentOS，Ubuntu 等等，你可以基于它们去创建自己版本的 Box，比如在虚拟机上安装一些软件，然后把它重新打包成 Box。</p>
<p>在 <code>vagrant添加 Box</code> ，Vagrant 提供的云服务 ,要把 Box 下载到本地的电脑上，交给 Vagrant 去管理，这样在创建虚拟机的时候，Vagrant 会复制一份你指定的 Box 到你的项目里面，这样你在这个虚拟机上的操作，就不会影响到其它的项目。</p>
<a id="more"></a>
<blockquote>
<p><a href="http://www.vagrantbox.es/" target="_blank" rel="noopener">http://www.vagrantbox.es/</a><br><a href="https://atlas.hashicorp.com/boxes/search" target="_blank" rel="noopener">https://atlas.hashicorp.com/boxes/search</a></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vagrant box add &#123;title&#125; &#123;box&#125;    <span class="comment"># title 为起的名字，box为在线URL地址，或者离线box路径</span></span><br></pre></td></tr></table></figure>
<p>这里<strong>可以安装很多个box，以服务不同的项目环境</strong>，可以随时通过<code>vagrant box remove {title}</code>,所有的命令可以通过vagrant -h 查看。</p>
<h3 id="查看已安装的box"><a href="#查看已安装的box" class="headerlink" title="查看已安装的box"></a>查看已安装的box</h3><p><code>vagrant box list</code></p>
<h3 id="初始化项目环境"><a href="#初始化项目环境" class="headerlink" title="初始化项目环境"></a>初始化项目环境</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 到项目目录</span></span><br><span class="line"><span class="built_in">cd</span> project</span><br><span class="line"><span class="comment"># 查看已经装的box</span></span><br><span class="line">vagrant box list</span><br><span class="line"></span><br><span class="line"><span class="comment"># 初始化项目，之后回生成Vagrantfile文件，这个是ruby写的配置文件有一些参数设置</span></span><br><span class="line"></span><br><span class="line">`vagrant init &#123;title&#125;`  ，title：为`config.vm.box`重命名，title可略则默认的`config.vm.box=”base”`</span><br><span class="line"></span><br><span class="line"><span class="comment"># demo</span></span><br><span class="line">vagrant box add centos6 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box</span><br></pre></td></tr></table></figure>
<h3 id="启动-并查看状态"><a href="#启动-并查看状态" class="headerlink" title="启动 并查看状态"></a>启动 并查看状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vagrant up</span><br><span class="line"></span><br><span class="line">vagrant status</span><br></pre></td></tr></table></figure>
<h3 id="控制虚拟机"><a href="#控制虚拟机" class="headerlink" title="控制虚拟机"></a>控制虚拟机</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># mac</span></span><br><span class="line">vagrant ssh</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># windows 用其他ssh链接工具</span></span><br><span class="line"></span><br><span class="line"> Host：127.0.0.1</span><br><span class="line"> Port：2200</span><br><span class="line"> Username：vagrant</span><br><span class="line"> password：vagrant</span><br></pre></td></tr></table></figure>
<h3 id="打包分发，生成box供其他人使用"><a href="#打包分发，生成box供其他人使用" class="headerlink" title="打包分发，生成box供其他人使用"></a>打包分发，生成box供其他人使用</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">vagrant package</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 之后别人拿到box，相当于安装了一个box又 ，之后就直接初始化这个</span></span><br><span class="line">vagrant box add hahaha ~/box/package.box  <span class="comment"># 添加 package.box 镜像并命名为 hahaha</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">cd</span> ~/dev  <span class="comment"># 切换到项目目录,之后初始化</span></span><br><span class="line">vagrant init hahaha</span><br></pre></td></tr></table></figure>
<h3 id="集成预安装"><a href="#集成预安装" class="headerlink" title="集成预安装"></a>集成预安装</h3><p>你会发现每次都修改了一点点内容，再打包分发给其他用户其实很麻烦，为此 Vagrant 还提供了更为便捷的预安装定制。打开 Vagrantfile 文件末尾处有下面被注释的代码：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">config.vm.provision <span class="string">"shell"</span>, inline: &lt;&lt;-SHELL</span><br><span class="line">   apt-get update</span><br><span class="line">   apt-get install -y apache2</span><br><span class="line">SHELL</span><br></pre></td></tr></table></figure></p>
<p>如果你不是初次运行，同时又修改了这里的命令，想让系统再次运行这里面的命令，你可以使用 <code>vagrant reload --provision</code> 进行重载。所以在这种情况下，你只要将 Vagrantfile 共享给团队的其他成员就可以了，其他成员运行相同的命令即可。更多<a href="https://www.vagrantup.com/docs/provisioning/" target="_blank" rel="noopener">参考</a></p>
<p>你还可以把要运行的命令单独写在一个文件里存放在相同的目录下，比如 bootstrap.sh：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/env bash</span></span><br><span class="line"></span><br><span class="line">apt-get update</span><br><span class="line">apt-get install -y apache2</span><br><span class="line"><span class="keyword">if</span> ! [ -L /var/www ]; <span class="keyword">then</span></span><br><span class="line">  rm -rf /var/www</span><br><span class="line">  ln -fs /vagrant /var/www</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<p>然后在 Vagrantfile 里这样添加：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Vagrant.configure(<span class="string">"2"</span>) <span class="keyword">do</span> |config|</span><br><span class="line">  config.vm.box = <span class="string">"hashicorp/precise64"</span></span><br><span class="line">  ...</span><br><span class="line"></span><br><span class="line">  config.vm.provision <span class="string">"shell"</span>, path: <span class="string">"bootstrap.sh"</span>  <span class="comment"># 添加这行</span></span><br><span class="line">end</span><br></pre></td></tr></table></figure></p>
<p><a href="https://www.vagrantup.com/docs/cli/index.html" target="_blank" rel="noopener">更多参考</a></p>
<h3 id="yii2-的vagrant-的-DEMO-配置"><a href="#yii2-的vagrant-的-DEMO-配置" class="headerlink" title="yii2 的vagrant 的 DEMO  配置"></a>yii2 的vagrant 的 DEMO  配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">vagrant box add ubuntu/trusty64</span><br><span class="line"><span class="comment"># 如果已经有了vagrantfile文件就不用 vargrant init 了</span></span><br><span class="line"></span><br><span class="line">vagrant up</span><br></pre></td></tr></table></figure>
<p>Vagrantfile 配置文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">require &apos;yaml&apos;</span><br><span class="line">require &apos;fileutils&apos;</span><br><span class="line"></span><br><span class="line">domains = &#123;</span><br><span class="line">  frontend: &apos;y2aa-frontend.dev&apos;,</span><br><span class="line">  backend:  &apos;y2aa-backend.dev&apos;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">config = &#123;</span><br><span class="line">  local: &apos;./vagrant/config/vagrant-local.yml&apos;,</span><br><span class="line">  example: &apos;./vagrant/config/vagrant-local.example.yml&apos;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># copy config from example if local config not exists</span><br><span class="line">FileUtils.cp config[:example], config[:local] unless File.exist?(config[:local])</span><br><span class="line"># read config</span><br><span class="line">options = YAML.load_file config[:local]</span><br><span class="line"></span><br><span class="line"># check github token</span><br><span class="line">if options[&apos;github_token&apos;].nil? || options[&apos;github_token&apos;].to_s.length != 40</span><br><span class="line">  puts &quot;You must place REAL GitHub token into configuration:\n/yii2-app-advancded/vagrant/config/vagrant-local.yml&quot;</span><br><span class="line">  exit</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line"># vagrant configurate</span><br><span class="line">Vagrant.configure(2) do |config|</span><br><span class="line">  # select the box</span><br><span class="line">  config.vm.box = &apos;ubuntu/trusty64&apos;</span><br><span class="line"></span><br><span class="line">  # should we ask about box updates?</span><br><span class="line">  config.vm.box_check_update = options[&apos;box_check_update&apos;]</span><br><span class="line"></span><br><span class="line">  config.vm.provider &apos;virtualbox&apos; do |vb|</span><br><span class="line">    # machine cpus count</span><br><span class="line">    vb.cpus = options[&apos;cpus&apos;]</span><br><span class="line">    # machine memory size</span><br><span class="line">    vb.memory = options[&apos;memory&apos;]</span><br><span class="line">    # machine name (for VirtualBox UI)</span><br><span class="line">    vb.name = options[&apos;machine_name&apos;]</span><br><span class="line">  end</span><br><span class="line"></span><br><span class="line">  # machine name (for vagrant console)</span><br><span class="line">  config.vm.define options[&apos;machine_name&apos;]</span><br><span class="line"></span><br><span class="line">  # machine name (for guest machine console)</span><br><span class="line">  config.vm.hostname = options[&apos;machine_name&apos;]</span><br><span class="line"></span><br><span class="line">  # network settings</span><br><span class="line">  config.vm.network &apos;private_network&apos;, ip: options[&apos;ip&apos;]</span><br><span class="line"></span><br><span class="line">  # sync: folder &apos;yii2-app-advanced&apos; (host machine) -&gt; folder &apos;/app&apos; (guest machine)</span><br><span class="line">  config.vm.synced_folder &apos;./&apos;, &apos;/app&apos;, owner: &apos;vagrant&apos;, group: &apos;vagrant&apos;</span><br><span class="line"></span><br><span class="line">  # disable folder &apos;/vagrant&apos; (guest machine)</span><br><span class="line">  config.vm.synced_folder &apos;.&apos;, &apos;/vagrant&apos;, disabled: true</span><br><span class="line"></span><br><span class="line">  # hosts settings (host machine)</span><br><span class="line">  config.vm.provision :hostmanager</span><br><span class="line">  config.hostmanager.enabled            = true</span><br><span class="line">  config.hostmanager.manage_host        = true</span><br><span class="line">  config.hostmanager.ignore_private_ip  = false</span><br><span class="line">  config.hostmanager.include_offline    = true</span><br><span class="line">  config.hostmanager.aliases            = domains.values</span><br><span class="line"></span><br><span class="line">  # provisioners</span><br><span class="line">  config.vm.provision &apos;shell&apos;, path: &apos;./vagrant/provision/once-as-root.sh&apos;, args: [options[&apos;timezone&apos;]]</span><br><span class="line">  config.vm.provision &apos;shell&apos;, path: &apos;./vagrant/provision/once-as-vagrant.sh&apos;, args: [options[&apos;github_token&apos;]], privileged: false</span><br><span class="line">  config.vm.provision &apos;shell&apos;, path: &apos;./vagrant/provision/always-as-root.sh&apos;, run: &apos;always&apos;</span><br><span class="line"></span><br><span class="line">  # post-install message (vagrant console)</span><br><span class="line">  config.vm.post_up_message = &quot;Frontend URL: http://#&#123;domains[:frontend]&#125;\nBackend URL: http://#&#123;domains[:backend]&#125;&quot;</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#!/usr/bin/env bash</span><br><span class="line"></span><br><span class="line">#== Import script args ==</span><br><span class="line"></span><br><span class="line">github_token=$(echo &quot;$1&quot;)</span><br><span class="line"></span><br><span class="line">#== Bash helpers ==</span><br><span class="line"></span><br><span class="line">function info &#123;</span><br><span class="line">  echo &quot; &quot;</span><br><span class="line">  echo &quot;--&gt; $1&quot;</span><br><span class="line">  echo &quot; &quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#== Provision script ==</span><br><span class="line"></span><br><span class="line">info &quot;Provision-script user: `whoami`&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Configure composer&quot;</span><br><span class="line">composer config --global github-oauth.github.com $&#123;github_token&#125;</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Install plugins for composer&quot;</span><br><span class="line">composer global require &quot;fxp/composer-asset-plugin:~1.1.1&quot; --no-progress</span><br><span class="line"></span><br><span class="line">info &quot;Install codeception&quot;</span><br><span class="line">composer global require &quot;codeception/codeception=2.0.*&quot; &quot;codeception/specify=*&quot; &quot;codeception/verify=*&quot; --no-progress</span><br><span class="line">echo &apos;export PATH=/home/vagrant/.config/composer/vendor/bin:$PATH&apos; | tee -a /home/vagrant/.profile</span><br><span class="line"></span><br><span class="line">info &quot;Install project dependencies&quot;</span><br><span class="line">cd /app</span><br><span class="line">composer --no-progress --prefer-dist install</span><br><span class="line"></span><br><span class="line">info &quot;Init project&quot;</span><br><span class="line">./init --env=Development --overwrite=y</span><br><span class="line"></span><br><span class="line">info &quot;Apply migrations&quot;</span><br><span class="line">./yii migrate &lt;&lt;&lt; &quot;yes&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Create bash-alias &apos;app&apos; for vagrant user&quot;</span><br><span class="line">echo &apos;alias app=&quot;cd /app&quot;&apos; | tee /home/vagrant/.bash_aliases</span><br><span class="line"></span><br><span class="line">info &quot;Enabling colorized prompt for guest console&quot;</span><br><span class="line">sed -i &quot;s/#force_color_prompt=yes/force_color_prompt=yes/&quot; /home/vagrant/.bashrc</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">	#!/usr/bin/env bash</span><br><span class="line"></span><br><span class="line">#== Import script args ==</span><br><span class="line"></span><br><span class="line">timezone=$(echo &quot;$1&quot;)</span><br><span class="line"></span><br><span class="line">#== Bash helpers ==</span><br><span class="line"></span><br><span class="line">function info &#123;</span><br><span class="line">  echo &quot; &quot;</span><br><span class="line">  echo &quot;--&gt; $1&quot;</span><br><span class="line">  echo &quot; &quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#== Provision script ==</span><br><span class="line"></span><br><span class="line">info &quot;Provision-script user: `whoami`&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Allocate swap for MySQL 5.6&quot;</span><br><span class="line">fallocate -l 2048M /swapfile</span><br><span class="line">chmod 600 /swapfile</span><br><span class="line">mkswap /swapfile</span><br><span class="line">swapon /swapfile</span><br><span class="line">echo &apos;/swapfile none swap defaults 0 0&apos; &gt;&gt; /etc/fstab</span><br><span class="line"></span><br><span class="line">info &quot;Configure locales&quot;</span><br><span class="line">update-locale LC_ALL=&quot;C&quot;</span><br><span class="line">dpkg-reconfigure locales</span><br><span class="line"></span><br><span class="line">info &quot;Configure timezone&quot;</span><br><span class="line">echo $&#123;timezone&#125; | tee /etc/timezone</span><br><span class="line">dpkg-reconfigure --frontend noninteractive tzdata</span><br><span class="line"></span><br><span class="line">info &quot;Prepare root password for MySQL&quot;</span><br><span class="line">debconf-set-selections &lt;&lt;&lt; &quot;mysql-server-5.6 mysql-server/root_password password \&quot;&apos;&apos;\&quot;&quot;</span><br><span class="line">debconf-set-selections &lt;&lt;&lt; &quot;mysql-server-5.6 mysql-server/root_password_again password \&quot;&apos;&apos;\&quot;&quot;</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Update OS software&quot;</span><br><span class="line">apt-get update</span><br><span class="line">apt-get upgrade -y</span><br><span class="line"></span><br><span class="line">info &quot;Install additional software&quot;</span><br><span class="line">apt-get install -y git php5-curl php5-cli php5-intl php5-mysqlnd php5-gd php5-fpm nginx mysql-server-5.6</span><br><span class="line"></span><br><span class="line">info &quot;Configure MySQL&quot;</span><br><span class="line">sed -i &quot;s/.*bind-address.*/bind-address = 0.0.0.0/&quot; /etc/mysql/my.cnf</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Configure PHP-FPM&quot;</span><br><span class="line">sed -i &apos;s/user = www-data/user = vagrant/g&apos; /etc/php5/fpm/pool.d/www.conf</span><br><span class="line">sed -i &apos;s/group = www-data/group = vagrant/g&apos; /etc/php5/fpm/pool.d/www.conf</span><br><span class="line">sed -i &apos;s/owner = www-data/owner = vagrant/g&apos; /etc/php5/fpm/pool.d/www.conf</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Configure NGINX&quot;</span><br><span class="line">sed -i &apos;s/user www-data/user vagrant/g&apos; /etc/nginx/nginx.conf</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Enabling site configuration&quot;</span><br><span class="line">ln -s /app/vagrant/nginx/app.conf /etc/nginx/sites-enabled/app.conf</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Initailize databases for MySQL&quot;</span><br><span class="line">mysql -uroot &lt;&lt;&lt; &quot;CREATE DATABASE yii2advanced&quot;</span><br><span class="line">mysql -uroot &lt;&lt;&lt; &quot;CREATE DATABASE yii2_advanced_tests&quot;</span><br><span class="line">echo &quot;Done!&quot;</span><br><span class="line"></span><br><span class="line">info &quot;Install composer&quot;</span><br><span class="line">curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer</span><br></pre></td></tr></table></figure>
<p><a href="http://www.yiichina.com/tutorial/979" target="_blank" rel="noopener">示例参考</a><br><a href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md" target="_blank" rel="noopener">示例参考</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>分享即是成长</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat-reward-image.png" alt="changyuan 微信支付">
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    change
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://changyuan.github.io/2016/11/10/vagrant/" title="vagrant usage">http://changyuan.github.io/2016/11/10/vagrant/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/09/shell/" rel="next" title="shell 相关">
                <i class="fa fa-chevron-left"></i> shell 相关
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/23/npm-script/" rel="prev" title="npm-script">
                npm-script <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="changyuan">
            
              <p class="site-author-name" itemprop="name">changyuan</p>
              <p class="site-description motion-element" itemprop="description">不断升级自己的操作系统</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">69</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">26</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/changyuan" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-fab fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/changocean1" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-fab fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://weibo.com/changyuan2011" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-fab fa-weibo"></i>Weibo</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/changyuanyuan" target="_blank" title="Zhihu">
                      
                        <i class="fa fa-fw fa-fab fa-quora"></i>Zhihu</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://weibo.com/changyuan2011" title="Weibo" target="_blank">Weibo</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/changyuan" title="GitHub" target="_blank">GitHub</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#VirtualBox，-Vagrant"><span class="nav-number">1.</span> <span class="nav-text">VirtualBox， Vagrant</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装Box"><span class="nav-number">2.</span> <span class="nav-text">安装Box</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看已安装的box"><span class="nav-number">3.</span> <span class="nav-text">查看已安装的box</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#初始化项目环境"><span class="nav-number">4.</span> <span class="nav-text">初始化项目环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#启动-并查看状态"><span class="nav-number">5.</span> <span class="nav-text">启动 并查看状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#控制虚拟机"><span class="nav-number">6.</span> <span class="nav-text">控制虚拟机</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#打包分发，生成box供其他人使用"><span class="nav-number">7.</span> <span class="nav-text">打包分发，生成box供其他人使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#集成预安装"><span class="nav-number">8.</span> <span class="nav-text">集成预安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#yii2-的vagrant-的-DEMO-配置"><span class="nav-number">9.</span> <span class="nav-text">yii2 的vagrant 的 DEMO  配置</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">changyuan</span>

  
</div>









<!-- 网站运行时间的设置 -->
<span id="timeDate">载入天数...</span>
<span id="times">载入时分秒...</span>
<script>
    var now = new Date();
    function createtime() {
        var grt= new Date("08/08/2014 00:00:00");
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
        document.getElementById("timeDate").innerHTML = "本站已运行 "+dnum+" 天 ";
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒.";
    }
setInterval("createtime()",250);
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
